package com.silverhaze.diamond.client.ui.builders;

import com.silverhaze.diamond.client.Application;
import com.silverhaze.diamond.client.settings.SettingsStorage;
import com.silverhaze.diamond.client.ui.UIHelper;
import com.silverhaze.diamond.client.ui.UIStringConstants;
import com.silverhaze.diamond.client.ui.dialogs.AboutDialog;
import com.silverhaze.diamond.client.ui.dialogs.ChangePasswordDialog;
import com.silverhaze.diamond.client.ui.dialogs.StatisticsDialog;
import com.silverhaze.diamond.client.ui.frame.UsersFrame;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by Ivan Babanin (10:10:11 01.06.2008)
 * This part of code is strictly confidential.
 * © Magnificent Software Inc.
 */
public class MenuBarBuilder {
    public static JMenuBar build() {
        JMenuBar menu = new JMenuBar();

        JMenu systemMenu = new JMenu(UIStringConstants.SYSTEM);
        systemMenu.setMnemonic('S');

        // System->Administration
        if (SettingsStorage.getUserSettings().getCurrentUser().isAdmin()) {
            JMenu adminMenu = new JMenu(UIStringConstants.ADMINISTRATION);

            JMenuItem usersItem = new JMenuItem(UIStringConstants.USERS);
            usersItem.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    UsersFrame.showUsers();
                }
            });

            adminMenu.add(usersItem);
            systemMenu.add(adminMenu);
            adminMenu.setMnemonic('A');
        }

        // System->Profile
        JMenu profileMenu = initProfileMenu();
        profileMenu.setMnemonic('P');

        // Statistics
        JMenu statisticsMenu = new JMenu("Статистика");
        statisticsMenu.add(new JMenuItem(new AbstractAction("По сервисам") {
            public void actionPerformed(ActionEvent e) {
                new StatisticsDialog().setVisible(true);
            }
        }));

        // Help
        JMenu helpMenu = new JMenu("?");
        JMenuItem aboutHelpMenu = new JMenuItem(new AbstractAction(UIStringConstants.ABOUT_PROGRAM) {
            public void actionPerformed(ActionEvent e) {
                AboutDialog.showDialog();
            }
        });
        helpMenu.add(aboutHelpMenu);

        // System->Exit
        JMenuItem exitServiceMenu = new JMenuItem(UIStringConstants.EXIT);
        exitServiceMenu.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Application.finish();
            }
        });
        exitServiceMenu.setMnemonic('E');

        systemMenu.add(profileMenu);
        systemMenu.add(exitServiceMenu);

        menu.add(systemMenu);
        menu.add(statisticsMenu);
//        menu.add(helpMenu);

        return menu;
    }

    private static JMenu initProfileMenu() {
        JMenu profileMenu = new JMenu(UIStringConstants.PROFILE);

        JMenuItem passSettingsMenu = new JMenuItem(UIStringConstants.CHANGE_PASSWORD);
        passSettingsMenu.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ChangePasswordDialog.showDlg(UIHelper.getMainFrame());
            }
        });

        JMenuItem logoutServiceMenu = new JMenuItem(UIStringConstants.LOG_OUT);
        logoutServiceMenu.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Application.reLogin();
            }
        });

        profileMenu.add(passSettingsMenu);
        profileMenu.add(logoutServiceMenu);

        return profileMenu;
    }

}

